package com.daykw.xiaohongshu.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientOptions.Builder;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;

/**
 * MongoDB工具类 Mongo实例代表了一个数据库连接池，即使在多线程的环境中，一个Mongo实例对我们来说已经足够了<br>
 * 注意Mongo已经实现了连接池，并且是线程安全的。 <br>
 * 设计为单例模式， 因 MongoDB的Java驱动是线程安全的，对于一般的应用，只要一个Mongo实例即可，<br>
 * Mongo有个内置的连接池（默认为10个） 对于有大量写和读的环境中，为了确保在一个Session中使用同一个DB时，<br>
 * DB和DBCollection是绝对线程安全的<br>
 * 
 * @author ebin
 * @date 2015-5-29 上午11:49:49
 * @version 0.0.0
 * @Copyright (c)1997-2015 NavInfo Co.Ltd. All Rights Reserved.
 */
public enum MongoDBUtil {

	/**
	 * 定义一个枚举的元素，它代表此类的一个实例
	 */
	instance;
	// private static SystemConfig systemConfig = SystemConfig.getInstance();
	private final static Log log = LogFactory.getLog(MongoDBUtil.class);
	// private final static String db_ip = systemConfig.getMongodb_ip();
	// private final static int db_port = systemConfig.getMongodb_port();
	private MongoClient mongoClient;

	static {
		log.info("===============MongoDBUtil初始化========================");
		// 从配置文件中获取属性值
		ServerAddress serverAddress = new ServerAddress(Const.MDB_IP, Const.MDB_PORT);

		// 下面这句，是哪个库的用户，必须在其下面认证才行，我这个用户是root最高权限，所以在要admin下认证，这个地方折腾了好久：（
		MongoCredential credential = MongoCredential.createScramSha1Credential(Const.MDB_DB, Const.MDB_USER,
				Const.MDB_PWD.toCharArray());
		List<MongoCredential> credentials = new ArrayList<MongoCredential>();
		credentials.add(credential);

		instance.mongoClient = new MongoClient(serverAddress, credentials);

		// or, to connect to a replica set, with auto-discovery of the primary,
		// supply a seed list of members
		// List<ServerAddress> listHost = Arrays.asList(new
		// ServerAddress("localhost", 27017),new ServerAddress("localhost",
		// 27018));
		// instance.mongoClient = new MongoClient(listHost);

		// 大部分用户使用mongodb都在安全内网下，但如果将mongodb设为安全验证模式，就需要在客户端提供用户名和密码：
		// boolean auth = db.authenticate(myUserName, myPassword);
		Builder options = new MongoClientOptions.Builder();
		// options.autoConnectRetry(true);// 自动重连true
		// options.maxAutoConnectRetryTime(10); // the maximum auto connect
		// retry time
		options.connectionsPerHost(300);// 连接池设置为300个连接,默认为100
		options.connectTimeout(15000);// 连接超时，推荐>3000毫秒
		options.maxWaitTime(5000); //
		options.socketTimeout(0);// 套接字超时时间，0无限制
		options.threadsAllowedToBlockForConnectionMultiplier(5000);// 线程队列数，如果连接线程排满了队列就会抛出“Out
		// of
		// semaphores
		// to get
		// db”错误。
		options.writeConcern(WriteConcern.JOURNALED);//
		options.build();
	}

	// ------------------------------------共用方法---------------------------------------------------
	/**
	 * 获取DB实例 - 指定DB
	 * 
	 * @param dbName
	 * @return
	 */
	public MongoDatabase getDB(String dbName) {
		if (dbName != null && !"".equals(dbName)) {
			MongoDatabase database = mongoClient.getDatabase(dbName);
			return database;
		}
		return null;
	}

	/**
	 * 获取collection对象 - 指定Collection
	 * 
	 * @param collName
	 * @return
	 */
	public MongoCollection<Document> getCollection(String dbName, String collName) {
		if (null == collName || "".equals(collName)) {
			return null;
		}
		if (null == dbName || "".equals(dbName)) {
			return null;
		}
		MongoCollection<Document> collection = mongoClient.getDatabase(dbName).getCollection(collName);
		return collection;
	}

	/**
	 * 查询DB下的所有表名
	 */
	public List<String> getAllCollections(String dbName) {
		MongoIterable<String> colls = getDB(dbName).listCollectionNames();
		List<String> _list = new ArrayList<String>();
		for (String s : colls) {
			_list.add(s);
		}
		return _list;
	}

	/**
	 * 获取所有数据库名称列表
	 * 
	 * @return
	 */
	public MongoIterable<String> getAllDBNames() {
		MongoIterable<String> s = mongoClient.listDatabaseNames();
		return s;
	}

	/**
	 * 删除一个数据库
	 */
	public void dropDB(String dbName) {
		getDB(dbName).drop();
	}

	/**
	 * 查找对象 - 根据主键_id
	 * 
	 * @param collection
	 * @param id
	 * @return
	 */
	public Document findById(MongoCollection<Document> coll, String id) {
		ObjectId _idobj = null;
		try {
			_idobj = new ObjectId(id);
		} catch (Exception e) {
			return null;
		}
		Document myDoc = coll.find(Filters.eq("_id", _idobj)).first();
		return myDoc;
	}

	public Document findByOne(MongoCollection<Document> coll, String field, String value) {

		Document myDoc = coll.find(Filters.eq(field, value)).first();
		return myDoc;
	}

	/** 统计数 */
	public int getCount(MongoCollection<Document> coll) {
		int count = (int) coll.count();
		return count;
	}

	/** 条件查询 */
	public MongoCursor<Document> find(MongoCollection<Document> coll, Bson filter) {
		return coll.find(filter).iterator();
	}

	/** 分页查询 */
	public MongoCursor<Document> findByPage(MongoCollection<Document> coll, Bson filter, int pageNo, int pageSize) {
		Bson orderBy = new BasicDBObject("_id", 1);
		return coll.find(filter).sort(orderBy).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();
	}

	/** 分页查询 */
	public MongoCursor<Document> findByPage2(MongoCollection<Document> coll, int pageNo, int pageSize) {
		Bson orderBy = new BasicDBObject("_id", 1);
		return coll.find().sort(orderBy).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();
	}

	/**
	 * 通过ID删除
	 * 
	 * @param coll
	 * @param id
	 * @return
	 */
	public int deleteById(MongoCollection<Document> coll, String id) {
		int count = 0;
		ObjectId _id = null;
		try {
			_id = new ObjectId(id);
		} catch (Exception e) {
			return 0;
		}
		Bson filter = Filters.eq("_id", _id);
		DeleteResult deleteResult = coll.deleteOne(filter);
		count = (int) deleteResult.getDeletedCount();
		return count;
	}

	/**
	 * FIXME
	 * 
	 * @param coll
	 * @param id
	 * @param newdoc
	 * @return
	 */
	public Document updateById(MongoCollection<Document> coll, String id, Document newdoc) {
		ObjectId _idobj = null;
		try {
			_idobj = new ObjectId(id);
		} catch (Exception e) {
			return null;
		}
		Bson filter = Filters.eq("_id", _idobj);
		// coll.replaceOne(filter, newdoc); // 完全替代
		coll.updateOne(filter, new Document("$set", newdoc));
		return newdoc;
	}

	public Document updateByCnd(MongoCollection<Document> coll, Bson filter, Document newdoc) {

		coll.updateOne(filter, new Document("$set", newdoc));
		return newdoc;
	}

	public void dropCollection(String dbName, String collName) {
		getDB(dbName).getCollection(collName).drop();
	}

	/**
	 * 关闭Mongodb
	 */
	public void close() {
		if (mongoClient != null) {
			mongoClient.close();
			mongoClient = null;
		}
	}

	/**
	 * 测试入口
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Const.MDB_IP = "49.4.70.26";
		Const.MDB_PORT = 37158;
		Const.MDB_DB = "xhs";
		String collName = "article";
		MongoCollection<Document> coll = MongoDBUtil.instance.getCollection(Const.MDB_DB, collName);
		
		
		org.bson.Document newdoc = new org.bson.Document();
		
		newdoc.put("comments", 44);
		coll.updateOne(Filters.eq("aid", "5a23c602fb2a366268abb2f4"), new org.bson.Document("$set", newdoc));

		// 插入多条
//		for (int i = 1; i <= 4; i++) {
//			Document doc = MongoDBUtil.instance.findByOne(coll, "aid", "5937fa88d1d3b90c22cb03f6");
//			System.out.println(doc);
//		}

		// // 根据ID查询
		// String id = "589d2a8e778f8d8c94af93df";
		// Document doc = MongoDBUtil.instance.findById(coll, id);
		// System.out.println(doc);

		// 查询多个
		// MongoCursor<Document> cursor1 = coll.find(Filters.eq("name",
		// "zhoulf")).iterator();
		// while (cursor1.hasNext()) {
		// Document _doc = (Document) cursor1.next();
		// System.out.println(_doc.toString());
		// }
		// cursor1.close();

		// 查询多个
		// MongoCursor<Person> cursor2 = coll.find(Person.class).iterator();

		// 删除数据库
		// MongoDBUtil.instance.dropDB(dbName);

		// 删除表
		// MongoDBUtil.instance.dropCollection(dbName, collName);

		// 修改数据
		// String id = "589d2a8e778f8d8c94af93df";
		// Document newdoc = new Document();
		// newdoc.put("name", "时候");
		// MongoDBUtil.instance.updateById(coll, id, newdoc);

		// 统计表
		System.out.println(MongoDBUtil.instance.getCount(coll));

		// 查询所有
		// Bson filter = Filters.eq("likes", 100);
		// MongoDBUtil.instance.find(coll, filter);

	}

}
